package com.concurrent;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Job{
	private boolean stopAble;
	private int id;

	private int dealPoin;

	public Job(int id , boolean stopAble,int dealPoin) {
		super();
		this.stopAble = stopAble;
		this.id = id;
		this.dealPoin=dealPoin;
	}

	public int getDealPoin() {
		return dealPoin;
	}

	public void setDealPoin(int dealPoin) {
		this.dealPoin = dealPoin;
	}


	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public boolean isStopAble() {
		return stopAble;
	}

	public void setStopAble(boolean stopAble) {
		this.stopAble = stopAble;
	}
}

class Task implements Runnable{

	private Job job;

	Task(Job job){
		this.job = job;
	}

	@Override
	public void run() {
		int i = 0;
		while(!job.isStopAble() && i++<job.getDealPoin()){
			/*if(job.getId()==1){
				System.out.println(job.getId()+"---"+job.isStopAble()+" : "+i);
			}else{
				System.out.println(job.getId()+"-****-"+job.isStopAble()+" : "+i);
			}*/
			
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("job"+job.getId()+" finished");
		Executor.downLatch.countDown();
	}

}

class Executor{
	Map<Integer,Job> map = new ConcurrentHashMap<Integer, Job>();
	
	public static CountDownLatch downLatch;

	public void execute(Job job) throws InterruptedException{
		Job job1= new Job(1, false, 1000);
		Job job2= new Job(2, false, 1000);
		Job job3= new Job(3, false, 1000);
		Job job4= new Job(4, false, 1000);
		Job job5= new Job(5, false, 1000);
		Job job6= new Job(6, false, 1000);
		Job job7= new Job(7, false, 1000);
		Job job8= new Job(8, false, 1000);
		Job job9= new Job(9, false, 1000);
		Job job10= new Job(10, false, 1000);
		Job job11= new Job(11, false, 1000);
		map.put(1,job1);
		map.put(2,job2);
		map.put(3,job3);
		map.put(4,job4);
		map.put(5,job5);
		map.put(6,job6);
		map.put(7,job7);
		map.put(8,job8);
		map.put(9,job9);
		map.put(10,job10);
		map.put(11,job11);
		
		downLatch = new CountDownLatch(map.size());
		
		ExecutorService service = Executors.newFixedThreadPool(5);
		
		for(int i =1;i<=map.size();++i){
			service.execute(new Task(map.get(i)));
		}
		
		downLatch.await();
		service.shutdown();
		System.out.println(")))))");
		
	}

}

public class Demo01 {

	public static void main(String[] args) throws InterruptedException {
		Executor executor = new Executor();
		Job job1= new Job(1, false, 1000);
		Job job2= new Job(2, false, 1000);
		//Job job3= new Job(3, false, 1000);
		//Job job4= new Job(4, false, 1000);
		executor.execute(job1);
		//executor.execute(job2);
		//executor.execute(job3);
		//executor.execute(job4);
		
	//	Thread.sleep(100);
	//	job1.setStopAble(true);
		
		
	}

}
